﻿using System;
using System.Collections.Generic;
using System.Text;

namespace KhtApi.Model
{
    public class Pmc
    {
        public string PmcNo { get; set; }
        public DateTime CreateDate { get; set; }
        public string Planner { get; set; }
        public string SoNo { get; set; }
        /// <summary>
        /// 审核状态：已审、未审
        /// </summary>
        public ECheckStatus CheckStatus { get; set; }
        public string CheckStatusCn
        {
            get
            {
                return CheckStatus.ToString();
            }
            set
            {
                if (Enum.TryParse(value, out ECheckStatus status))
                {
                    CheckStatus = status;
                }
                else
                {
                    throw (new Exception("无法将输入的字符串转换成ECheckStatus，请确保输入的字符串为'已审、未审'"));
                }
            }
        }
        /// <summary>
        /// 生效状态：生效、未生效
        /// </summary>
        public EAffectStatus AffectStatus { get; set; }
        public string AffectStatusCn
        {
            get
            {
                return AffectStatus.ToString();
            }
            set
            {
                if(Enum.TryParse(value,out EAffectStatus status))
                {
                    AffectStatus = status;
                }
                else
                {
                    throw (new Exception("无法将输入的字符串转换成EAffectStatus，请确保输入的字符串为'生效、未生效'"));
                }
            }
        }
        public List<PmcProduct> Products { get; set; }
        public List<PmcMaterial> Materials { get; set; }
                
    }
    public enum ECheckStatus
    {
        已审,未审
    }
    public enum EAffectStatus
    {
        生效,未生效
    }
    public class PmcProduct
    {
        public string ItemCode { get; set; }
        public string Catagory1 { get; set; }
        public string Catagory2 { get; set; }
        public string Catagory3 { get; set; }
        public string ItemName { get; set; }
        public string ModelNo { get; set; }
        public string Spec { get; set; }
        public string Version { get; set; }
        public string Unit { get; set; }
        public double TotalQty { get; set; }
        public string SoNo { get; set; }

        /// <summary>
        /// 自产|外购|客供|委外
        /// </summary>
        public string OriginCn {
            get
            {
                return Origin.ToString();
            }
            set
            {
                if(Enum.TryParse(value,out EOrigin origin))
                {
                    Origin = origin;
                }
                else
                {
                    throw (new Exception("无法将输入的字符串转换成EOrigin产品来源类型！请确保输入的字符串为‘自产|外购|客供|委外’四种之一"));
                }
            }
        }
        public EOrigin Origin { get; set; }
      
        public int BomQty { get; set; }
    }
    public class PmcMaterial
    {
        public string PrdItemCode { get; set; }
        public string ItemCode { get; set; }
        public string Catagory1 { get; set; }
        public string Catagory2 { get; set; }
        public string Catagory3 { get; set; }
        public string ItemName { get; set; }
        public string ModelNo { get; set; }
        public string Spec { get; set; }
        public string Unit { get; set; }
        public double TotalQty { get; set; }
        public double StockQty { get; set; }
        public double LockQty { get; set; }
        public double TransitQty { get; set; }
        public double PlanningQty { get; set; }

        /// <summary>
        /// 自产|外购|客供|委外
        /// </summary>
        public EOrigin Origin { get; set; }
        /// <summary>
        /// 自产|外购|客供|委外
        /// </summary>
        public string OriginCn
        {
            get
            {
                return Origin.ToString();
            }
            set
            {
                if (Enum.TryParse(value, out EOrigin origin))
                {
                    Origin = origin;
                }
                else
                {
                    throw (new Exception("不支持的产品来源类型！请确保输入的字符串为‘自产|外购|客供|委外’四种之一"));
                }
            }
        }
        /// <summary>
        /// 用外挂生成时，可将关联订单号写入
        /// </summary>
        public string SoNo { get; set; }
        public PmcMaterial()
        {
            StockQty = 0;
            LockQty = 0;
            TransitQty = 0;
        }
        /// <summary>
        /// PMC分解的物料
        /// </summary>
        /// <param name="p">要分解的零部件</param>
        /// <param name="parentItemCode">上级编码</param>
        /// <param name="totalQty">需求数量</param>
        /// <param name="soNo">受订单号</param>
        /// <param name="stockQty">库存数量</param>
        /// <param name="tranmitQty">在途数量</param>
        /// <param name="lockQty">锁定数量</param>
        public PmcMaterial(ProductInfo p, double totalQty, string soNo, string parentItemCode,double stockQty=0,double tranmitQty=0,double lockQty=0)
        {
            TotalQty = totalQty;
            StockQty = stockQty;
            LockQty = lockQty;
            TransitQty = tranmitQty;
            PlanningQty = totalQty - StockQty - TransitQty + LockQty;
            PrdItemCode = parentItemCode;
            ItemCode = p.ItemCode;
            ItemName = p.Name;
            ModelNo = p.ModelNo;
            Spec = p.Spec;
            Catagory1 = p.Catagory1;
            Catagory2 = p.Catagory2;
            Catagory3 = p.Catagory3;
            Unit = p.Unit;
            SoNo = soNo;
            Origin = p.Origin;
        }
       
    }
    public enum EOrigin
    {
        自产,外购,客供,委外
    }
   
}
